package com.it135.citytraffic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.it135.citytraffic.mapper.RoadStatisticMapper;
import com.it135.citytraffic.mapper.RoadTimeStatisticMapper;
import com.it135.citytraffic.pojo.entity.Crossing;
import com.it135.citytraffic.mapper.CrossingMapper;
import com.it135.citytraffic.pojo.entity.RoadStatistic;
import com.it135.citytraffic.pojo.entity.RoadTimeStatistic;
import com.it135.citytraffic.service.ICrossingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CrossingServiceImpl extends ServiceImpl<CrossingMapper, Crossing> implements ICrossingService {

    @Autowired
    private CrossingMapper crossingMapper;

    @Autowired
    private RoadStatisticMapper roadStatisticMapper;

    @Autowired
    private RoadTimeStatisticMapper roadTimeStatisticMapper;

    /**
     * 状态更新
     *
     * @param crossing
     */
    @Override
    public void updateStatus(Crossing crossing) {
        crossingMapper.update(crossing);
    }

    /**
     * 路口预测
     *
     * @param id
     * @return
     */
    @Override
    public String predictCrossing(Long id) {
        String name = crossingMapper.predictName(id);
        Integer count_all = crossingMapper.predictCount(id);
        String[] block_arr = crossingMapper.predictBlock(id);
        int count = 0;
        for (int i = 0; i < count_all; i++) {
            double num = Double.parseDouble(block_arr[i].split("%")[0]);
            if (num >= 1.0) count++;
        }
        return name + "路口, 出现拥堵的概率为" + String.format("%.2f", count * 100.0 / count_all) + "%";
    }
    /**
     * 路段预测
     *
     * @param id
     * @return
     */
    @Override
    public String predictRoad(Long id) {
        List<RoadStatistic> roadStatistics = roadStatisticMapper.getRS(id);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < roadStatistics.size(); i++) {
            RoadStatistic rs = roadStatistics.get(i);
            Long rsId = rs.getId(); //rs的id
            String name = rs.getName(); //rs的名称
            String direction = rs.getDirection();
            Integer status = rs.getStatus();
            boolean flag = false;
            List<Long> count_all_id = roadStatisticMapper.getAllId(id, name, direction);
            int count_all = 0;
            for (int j = 0; j < count_all_id.size(); j++) {
                Long count_id = count_all_id.get(j);
                int count = roadTimeStatisticMapper.getAll(count_id);
                count_all += count;
            }
            List<RoadTimeStatistic> roadTimeStatistics = roadTimeStatisticMapper.getRTS(rsId); //获取rts对象集合
            if (status >= 3) {
                for (int j = 0; j < roadTimeStatistics.size(); j++) {
                    RoadTimeStatistic rts = roadTimeStatistics.get(j);
                    String time = rts.getTime();
                    Integer count = rts.getCount();
                    double result = (count * 1.0 / count_all) * 100;
                    if (result >= 40.0 && result <= 75.0) {
                        if (!flag) {
                            sb.append(name + "在" + direction + "方向上:");
                            flag = true;
                        }
                        if (status == 3) {
                            String resultBlock = String.format("%.1f", result) + "%"; //概率
                            sb.append("在" + time.split(",")[0] + "时到" + time.split(",")[1] + "时, 出现拥堵情况概率为" + resultBlock + ";<br>");
                        } else if (status == 4) {
                            String resultSerious = String.format("%.1f", result) + "%"; //概率
                            sb.append("在" + time.split(",")[0] + "时到" + time.split(",")[1] + "时, 出现严重拥堵情况概率为" + resultSerious + ";<br>");
                        }
                    }

                }
            }
        }
        return sb.toString();
    }

    @Override
    public Long insert(Crossing crossing) {
        crossingMapper.save(crossing);
        return crossing.getId();
    }
}
